//
//  TopicViewModel.swift
//  HoneyBee
//
//  Created by Smith_Yang on 2018/4/19.
//  Copyright © 2018 point. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift


class TopicViewModel {

    weak var vc: TopicViewController?
    var modelObserable : [TopicModel] =  [TopicModel]()
    var publishSubData = PublishSubject<mpLoadData>()
    fileprivate var page: Int = 1 //当前的页数
    fileprivate lazy var bag: DisposeBag =  DisposeBag()
    
    init() {
        publishSubData.subscribe(onNext: { [weak self] (type:mpLoadData) in
            self?.loadData(type: type)
        }).disposed(by: bag)
    }
}


extension TopicViewModel {

    func loadData(type:mpLoadData) {
        if type == .mpLoadDataUp {
            self.page = 1
            
            TempNetTool.post( kHost + kqueryTopicList + "".apiFOperator(time: "2", sign: "2") + "&pageNo=\(self.page)" + "&pageSize=10", parameters: nil, progress: nil, success: { [weak self] (result) in
                
                self?.vc?.hudHide()
                self?.vc?.tableView.mj_header.endRefreshing()

                if (result != nil) {
                    guard let resultDict = result as? [String : NSObject] else { return }
                    guard let dataArray = resultDict["data"] as? [[String : NSObject]] else { return }
                    let modelArr = TopicModel.tmSpeelBreakModelArr(dataArray) as! [TopicModel]
                    if (modelArr.count < 10){
                        self?.vc?.tableView.mj_footer.endRefreshingWithNoMoreData()
                    }
                    self?.modelObserable.removeAll()
                    self?.modelObserable =  (self?.modelObserable)! + modelArr
                    self?.vc?.tableView.reloadData()
                    self?.page = (self?.page)! + 1
                }
                
                
            }) {[weak self]   (error) in
                self?.vc?.hudHide()
                self?.vc?.tableView.mj_header.endRefreshing()
            }
            
        }else {
            
            
            TempNetTool.post( kHost + kqueryTopicList + "".apiFOperator(time: "2", sign: "2") + "&pageNo=\(self.page)" + "&pageSize=10", parameters: nil, progress: nil, success: { [weak self] (result) in
                
                self?.vc?.hudHide()
                self?.vc?.tableView.mj_footer.endRefreshing()

                if (result != nil) {
                    guard let resultDict = result as? [String : NSObject] else { return }
                    guard let dataArray = resultDict["data"] as? [[String : NSObject]] else { return }
                    let modelArr = TopicModel.tmSpeelBreakModelArr(dataArray) as! [TopicModel]
                    if (modelArr.count < 10){
                        self?.vc?.tableView.mj_footer.endRefreshingWithNoMoreData()
                    }

                    self?.modelObserable =  (self?.modelObserable)! + modelArr
                    
                    self?.vc?.tableView.reloadData()
                    
                    self?.page = (self?.page)! + 1
                }
                
                
            }) {[weak self]   (error) in
                self?.vc?.hudHide()
                self?.vc?.tableView.mj_footer.endRefreshing()
                
            }
            
        }
    }
}
